<?php
declare (strict_types=1);

namespace app\promotion\logic;

use app\promotion\model\Promotion as PromotionModel;

/**
 * 活动逻辑层
 * @method join(string $string, string $string1)
 * @method whereId(int $promotion_id)
 * @method product(int $product)
 * @method current()
 */
class Promotion extends PromotionModel
{

    /**
     * 获取产品当前活动信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param int $product_type [产品类型ID]
     * @param int $product      [产品ID]
     * @return mixed [type]               [description]
     */
    public function getProductCurrentPromotion(int $product_type, int $product)
    {
        return $this->product($product)->productType($product_type)->current()->findOrEmpty()->append(['running_status', 'start_countdown', 'end_countdown', 'type_text']);
    }

    /**
     * 获取产品当前活动类型
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param int $product_type [产品类型ID]
     * @param int $product      [产品ID]
     * @return int [type]               [description]
     */
    public function getProductCurrentPromotionType(int $product_type, int $product): int
    {
        $type = $this->product($product)->productType($product_type)->current()->value('type');

        return intval($type ?? 0);
    }

    /**
     * 活动类型
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-15
     *
     * @param int $promotion_id [description]
     * @return int [type]               [description]
     */
    public function getPromotionType(int $promotion_id): int
    {
        $type = $this->whereId($promotion_id)->value('type');

        return $type * 1;
    }

    /**
     * 获取当前有效的活动信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-16
     *
     * @param int $promotion_id [description]
     * @return mixed [type]               [description]
     */
    public function getPositivePromotionInfo(int $promotion_id)
    {
        $data = $this->current()->findOrFail($promotion_id)->append(['running_status', 'start_countdown', 'end_countdown', 'type_text']);
        return $data->visible(['id', 'type', 'price', 'type_text', 'end_time']);
    }

    /**
     * 用户订单页获取有效的活动
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-07
     *
     * @param int $product_type [产品类型ID]
     * @param int $product      [产品ID]
     * @return array [type]               [description]
     */
    public function getProductPositivePromotion(int $product_type, int $product): array
    {
        $data = $this->getProductCurrentPromotion($product_type, $product);
        $id   = intval($data->id);
        $data->visible(['id', 'type', 'price', 'type_text', 'end_time', 'expiry_time']);
        $empty = [];
        if ($data->isEmpty()) return $empty;
        switch ($data->getAttr('type')) {
            case 3:
                // 砍价成功
                $done = app(PromotionBargain::class)->promotionId($id)->sponsor()->value('done');
                return $done ? $data->toArray() : $empty;
            case 4:
                // 入团订单
                $tuan = app(PromotionTuan::class)->getUserPositiveTuan($id);
                if ($tuan->isEmpty()) return $empty;
                $data->expiry_time = $tuan->expiry_time;
                return $data->toArray();

            default:
                return $data->toArray();
        }
    }

    /**
     * 删除用户活动信息，用于取消订单
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-09
     */
    public function deleteUserPromotion(int $id, int $user_id)
    {
        // 活动信息
        $promotion = app(Promotion::class)->withTrashed()->findOrEmpty($id);
        if (!$promotion->isEmpty()) {
            switch ($promotion->getAttr('type')) {
                case 3:
                    app(PromotionBargain::class)->deleteUserBargain($id, $user_id);
                    break;

                case 4:
                    app(PromotionTuan::class)->deleteUserTuan($id, $user_id);
                    break;

                default:

                    break;
            }
        }
    }

}
